//
//  VerifyLoginBackView.m
//  Runner
//
//  Created by Yunxi on 2021/8/27.
//

#import "VerifyLoginBackView.h"
#import "DeviceConfig.h"
#import "OtherWayLoginView.h"
#import <AVFoundation/AVFoundation.h>
#import <UmengVertifyPlugin.h>

@interface VerifyLoginBackView()

@property (nonatomic,strong) AVPlayer *player;//播放器对象
@property (nonatomic,strong) AVPlayerItem *currentPlayerItem;

@end

@implementation VerifyLoginBackView

- (instancetype)init{
    if (self = [super init]) {

        self.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
//        self.backgroundColor = [UIColor redColor];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayDidEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:self.player.currentItem];

        //视频背景
        [self setUpPlayer];
        
        //模糊
        UIView *bgView  = [[UIView alloc] initWithFrame:self.frame];
        [bgView setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.5]];
        [self addSubview:bgView];
        
        //右上角关闭按钮
        UIButton *closeButton = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth-41*ScaleW, 58*ScaleH, 26*ScaleW, 26*ScaleW)];
        [closeButton setHidden:![UmengVertifyPlugin getCloseButtonIsShow]];
//        [closeButton setBackgroundColor:[UIColor redColor]];
        [closeButton addTarget:self action:@selector(clickCloseButton) forControlEvents:UIControlEventTouchUpInside];
        [closeButton setImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];
        [self addSubview:closeButton];
        
        //其他登录方式
        OtherWayLoginView *otherView = [[OtherWayLoginView alloc] initWithFrame:CGRectMake(0, 514*ScaleH, ScreenWidth, 42)];
        [self addSubview:otherView];
    }
    
    return self;
}

- (void)clickCloseButton{
    [UmengVertifyPlugin clickOtherLogin:ClosePage];
}

- (void)setUpPlayer{
    //本地视频路径
    NSString* localFilePath=[[NSBundle mainBundle]pathForResource:@"aaaaa" ofType:@"mp4"];

    NSURL *localVideoUrl = [NSURL fileURLWithPath:localFilePath];
        
    AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithURL:localVideoUrl];
    self.currentPlayerItem = playerItem;
    self.player = [[AVPlayer alloc] initWithPlayerItem:playerItem];
    self.player.volume = 0;
    AVPlayerLayer *avLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
    avLayer.videoGravity = AVLayerVideoGravityResize;
    avLayer.frame = self.bounds;
    [self.layer addSublayer:avLayer];
    
    [self.player play];
}

- (void)dealloc {
    NSLog(@"是否可以销毁");

    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

// 视频循环播放
- (void)moviePlayDidEnd:(NSNotification*)notification{

    AVPlayerItem*item = [notification object];

    [item seekToTime:kCMTimeZero];

    [self.player play];

}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
